% The function models any series as an AR(p) process.
% The lag-length p is selected on the basis of BIC
% Jean Boivin, Marc Giannoni, Ilian Mihov
%
% Syntax:
%  [rho,imp] = sacc(y,maxp,nir)
% 
% where the inputs are:     
%   y = a vector with the series to be modeled
%   maxp = number of lags in AR(p)
%   nir = number of periods in the impulse response function 
%
% and the outputs are:
%   rho = degree of persistence (correlation if p=1)
%   imp = impulse response function for a shock in the AR(p) process

function [rho,imp] = sacc(y,maxp,nir);

ly  = ones(size(y,1),1);

%construct the lag variables
for i  = 1:maxp;
    ly = [ly lagn(y,i)];
end

yt  =  y(maxp+1:end,:);
lyt = ly(maxp+1:end,:);

% compute the impulse response function to sector-specific shocks
b = olss(yt,lyt(:,1:maxp+1));     %fixed number of lags
rho = sum(b(2:end));
imp=repmat(NaN,1,nir);
ly=zeros(1,maxp);                 %fixed number of lags
y=-std(y);
imp(1,1)=y;
ly(1,1)=y;
for i=2:48;
    y=ly*b(2:end);
    imp(1,i)=y;
    ly=[y ly(1:end-1)];
end
    